package cn.melonlib.cores.aop.comp.web;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

@Component
@Data
public class WebInfo {

    public static final Pattern hasParamsUrlRegex=Pattern.compile("^.*\\?.*$");

    private final ThreadLocal<ServerInfo> serverInfoThreadLocal=new ThreadLocal<>();

    private final ThreadLocal<ClientInfo> clientInfoThreadLocal=new ThreadLocal<>();

    @Value("${sso.service_origin:http://localhost:8080}")
    private String serviceOrigin;

    public ServerInfo getServerInfo(){
        return serverInfoThreadLocal.get();
    }

    public void setServerInfo(ServerInfo serverInfo){
        serverInfoThreadLocal.set(serverInfo);
    }

    public void removeServerInfo(){
        serverInfoThreadLocal.remove();
    }

    public ClientInfo getClientInfo(){
        return clientInfoThreadLocal.get();
    }


    public void setClientInfo(ClientInfo clientInfo){
        clientInfoThreadLocal.set(clientInfo);
    }
    public void removeClientInfo(){
        clientInfoThreadLocal.remove();
    }
    @Data
    public static class ServerInfo{

        public ServerInfo() {
        }

        public ServerInfo(String protocol,String domain, String ip, Integer port, String requestPath, String method) {
            this.domain = domain;
            this.ip = ip;
            this.port = port;
            this.requestPath = requestPath;
            this.method = method;
            this.prefix=protocol+"://"+domain;
        }
        private String domain;
        private String ip;
        private Integer port;
        private String requestPath;
        private String method;
        private String prefix;
    }

    @Data
    public static class ClientInfo{

        public ClientInfo(String ip, Integer port, String userAgent) {
            this.ip = ip;
            this.port = port;
            this.userAgent = userAgent;
        }

        private String ip;
        private Integer port;
        private String userAgent;
    }

}
